/**
 * Created by zhangguoqing Date:2016/6/28
 */
var currentId;
var ltsTable;
$(document).ready(function(){
	var url = getRoot() + "/role/find";
	ltsTable = $("#role-table-container").ltsTable({
        url: url,
        templateId: 'role-table'
    });
	ltsTable.post({},1);
	//编辑 删除按钮
	LTS.colFormatter.optFormat = function (v, row) {
        return '' + updateBtn + removeBtn;
    }
	
	clickRemoveBtn();
	clickConfirmRemoveBtn();
	searchClickButton();
	updateClick();
});

/**
 * 点击删除按钮
 */
function clickRemoveBtn() {
    $(document).on("click", ".remove-btn", function () {
        currentId = getCurrentId($(this));
        $("#remove-tip-modal").modal("show");
    });
}

/**
 * 点击确认删除按钮
 */
function clickConfirmRemoveBtn() {
    $(document).on("click", "#confirm-remove-btn", function () {
        $("#cancel-remove-btn").trigger("click");
        var params = {};
        params['id'] = parseInt($.trim(currentId));
        var url = getUrlBySuffix("/role/remove");
        $.postHttp(params, url, function (json) {
            if (json && json.success) {
                swal("成功", json['message'], "success");
                ltsTable.post({},1);
            } else if (json) {
                swal("失败", json['message'], 'error');
            } else {
                swal("失败", "未能从服务器获取正确信息", 'error');
            }
        });
        currentId = "";
    });
}

/**
 * 获取当前操作任务的Id
 * @param e
 */
function getCurrentId(ele) {
    var id = ele.parent().parent().find(".id-tr").find(".id-span").html();
    return id;
}

/**
 *搜索按钮 
 */
function searchClickButton(){
	$(document).on("click", "#searchBtn", function () {
        var params = {};
        $.each($('#role-query-form').find(".form-control"), function () {
            var name = $(this).attr("name");
            if(isNull(name)){
                return true;
            }
            var value = $(this).val();
            if(!isNull(value)){
                value = $.trim(value);
                params[name] = value;
            }
        });
        ltsTable.post(params, 1);
    });
}

/**
 * 角色编辑
 **/
function updateClick(){
	$(document).on("click",".update-btn",function(){
		currentId = getCurrentId($(this));
		window.location.href = getRoot() + "/role/toUpdatePage?currentId=" + currentId;
	});
}